﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class cobranza_ImpresionCarta : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String pValor = Request.QueryString["idSeparacion"];
        hdfImprimir.Value = pValor;
        String pIdProforma = Request.QueryString["idProyecto"];
        hdfIdProyecto.Value = pIdProforma;
        String pIdEtapa = Request.QueryString["idEtapa"];
        hdfIdEtapa.Value = pIdEtapa;
        String pDescEmpresa = Request.QueryString["descEmpresa"];
        hdfEmpresa.Value = pDescEmpresa;
    }



    protected void lnbImprimir_Click(object sender, EventArgs e)
    {
        String empresa = Convert.ToString(hdfEmpresa.Value);

        //String strConnection = "Provider=SQLNCLI;Server=SERVIDOR;Database=EMPRESA;Uid=USUARIO; Pwd=CONTRASEÑA;";
        String strConnection = ConfigurationManager.ConnectionStrings[empresa.ToUpper()].ConnectionString;

        //MPE OleDbConnection Connection = new OleDbConnection(strConnection);
        SqlConnection Connection = new SqlConnection(strConnection);

        //MPE OleDbDataAdapter DA = new OleDbDataAdapter("usp_venta_rptVenta", Connection);
        SqlDataAdapter DA = new SqlDataAdapter("usp_carta_rptcarta", Connection);

        DA.SelectCommand.CommandType = CommandType.StoredProcedure;
        DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
        DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
        DA.SelectCommand.Parameters.Add("@idSeparacionVenta", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
        DataSet DS = new DataSet();
        DA.Fill(DS, "T0");

        switch (empresa.ToLower())
        {
            case "epazcenpr":
                DA = new SqlDataAdapter("usp_carta_inmuebles", Connection);
                DA.SelectCommand.CommandType = CommandType.StoredProcedure;
                DA.SelectCommand.Parameters.Add("@idProyecto", SqlDbType.Int).Value = Convert.ToInt32(hdfIdProyecto.Value);
                DA.SelectCommand.Parameters.Add("@idEtapa", SqlDbType.Int).Value = Convert.ToInt32(hdfIdEtapa.Value);
                DA.SelectCommand.Parameters.Add("@idSeparacionVenta", SqlDbType.Int).Value = Convert.ToInt32(hdfImprimir.Value);
                DA.Fill(DS, "T1");
                break;
        }

        String repFilePath = null;
        repFilePath = Server.MapPath("~/cobranza/rptCarta/carta" + empresa.ToString() + hdfIdProyecto.Value.ToString() + ".rpt");
        if (System.IO.File.Exists(repFilePath))
        {
            ReportDocument repDoc1 = new ReportDocument();
            repDoc1.Load(repFilePath);
            repDoc1.SetDataSource(DS.Tables["T0"]);

            ReportDocument repDoc = repDoc1;
            Response.Buffer = false;
            Response.ClearContent();
            Response.ClearHeaders();

            switch (empresa.ToLower())
            {
                case "epazcenpr":
                    SubreportObject subReportObject;
                    ReportDocument subReporte;
                    subReportObject = repDoc1.ReportDefinition.ReportObjects["Subreport2"] as SubreportObject;
                    subReporte = repDoc1.OpenSubreport(subReportObject.SubreportName);
                    subReporte.SetDataSource(DS.Tables["T1"]);
                    break;
            }

            try
            {
                repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Carta");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                ex = null;
            }
        }
    }
}
